package com.aaa.shuke2356.controller;

import com.aaa.shuke2356.demos.web.User;
import com.aaa.shuke2356.entity.Status;
import com.aaa.shuke2356.service.StatusService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Status)表控制层
 *
 * @author teacherChen
 * @since 2024-11-20 16:01:51
 */
@RestController
@RequestMapping("status")
public class StatusController {
    /**
     * 服务对象
     */
    @Resource
    private StatusService statusService;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 分页查询
     *
     * @param status 筛选条件
     * @param page      当前第几页
     * @param size      一页显示多少条数据
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Status>> queryByPage(Status status, int page ,int size) {
        //因为接口默认的page是从0开始的，所以接收到page参数之后，需要减去1
        PageRequest pageRequest = PageRequest.of(page-1, size);
        return ResponseEntity.ok(this.statusService.queryByPage(status, pageRequest));
    }
    @GetMapping("/list")
    public ResponseEntity<Page<Status>> queryByPageList(Status status, int page ,int size) {
        //因为接口默认的page是从0开始的，所以接收到page参数之后，需要减去1
        PageRequest pageRequest = PageRequest.of(page-1, size);
        return ResponseEntity.ok(this.statusService.queryByPageList(status, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Status> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.statusService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param status 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Status> add(Status status) {
        return ResponseEntity.ok(this.statusService.insert(status));
    }

    /**
     * 编辑数据
     *
     * @param status 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Status> edit(Status status) {
        return ResponseEntity.ok(this.statusService.update(status));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.statusService.deleteById(id));
    }

    /**
     * 接收agent发送的数据
     * @param status
     * @return
     */
    @RequestMapping("/add_status")
    @ResponseBody
    public String addStatus(@RequestBody Status status) {
        System.out.println("接收的数据："+status.toString());
        //将status对象转换成json字符串
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String string = objectMapper.writeValueAsString(status);
            //将指标数据保存到kafka中
            kafkaTemplate.send("chenjian",string);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return "接收到的参数: " + status.toString();
    }

    @RequestMapping("/getServerByIp")
    @ResponseBody
    public List<Status> getServerByIp(String serverIp){
        List<Status> statusList = statusService.getServerByIp(serverIp);
        return statusList;
    }

}

